package a10_动态规划;

import java.util.Arrays;

/**
 * <p>
 * a16_目标和复习4
 * </p>
 *
 * @author flyduck
 * @since 2025/4/11
 */
public class a16_目标和复习4 {
    public static void main(String[] args) {
        findTargetSumWays(new int[]{1}, 1);
    }
    //x - y = target
    //x + y = sum
    //x - (sum - x) = target
    //x = target + sum /2
    public static int findTargetSumWays(int[] nums, int target) {
        int sum = Arrays.stream(nums).sum();
        if((target + sum) % 2 != 0){
            return 0;
        }
        target = (target + sum) / 2;
        if(target < 0){
            return 0;
        }

        int[] dp = new int[target+1];
        dp[0] = 1;
        for (int i = 0; i < nums.length; i++) {
            for (int j = target; j >= nums[i]; j--) {
                dp[j] = dp[j]+dp[j-nums[i]];
            }
        }

        return dp[target];
    }
}
